#ifndef __UART_PORT_H__
#define __UART_PORT_H__

#include "main.h"
#include <stdint.h>
#include <string.h>

// 环形缓冲区结构体
typedef struct
{
    uint8_t *buffer;
    uint16_t size;
    uint16_t head;
    uint16_t tail;
    uint16_t count;
} ring_buffer_t;

// 串口设备结构体
typedef struct
{
    UART_HandleTypeDef *huart;
    ring_buffer_t rx_buffer;
    ring_buffer_t tx_buffer;
    volatile uint8_t tx_busy;
} uart_device_t;

// 函数声明
void uart_async_init(uart_device_t *dev, UART_HandleTypeDef *huart, uint8_t *rx_buf, uint16_t rx_size, uint8_t *tx_buf, uint16_t tx_size);
unsigned int uart_write(uart_device_t *dev, const void *buf, unsigned int len);
unsigned int uart_read(uart_device_t *dev, void *buf, unsigned int len);
void uart_rx_callback(uart_device_t *dev);
void uart_tx_callback(uart_device_t *dev);

// 外部需要实现的函数指针
extern unsigned int (*write)(const void *buf, unsigned int len);
extern unsigned int (*read)(void *buf, unsigned int len);

#endif
